﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using SocialPlatformSDK;
using SocialPlatformSDK.Model;
using SocialPlatformSDK.Services;
using SocialPlatformSDK.Services.Util;

namespace SocialPlatformSDK.ViewModel
{
    public class TimelineViewModel : BaseViewModel
    {
        /// <summary>
        /// 实例化时间线相关 API 服务.
        /// </summary>
        TencentSocFactory tencentfactory; 
        TencentAPIService<TencentServiceArgument> _tencentAPIService;

        SinaSocFactory sinafactory;
        SinaAPIService<SinaServiceArgument> _sinaAPIService;

        RenRenSocFactory renrenfactory;
        RenRenAPIService<RenRenServiceArgument> _renrenAPIService;
        /// <summary>
        /// 实例化帐户相关 API 服务.
        /// </summary>
        //private UserService userService = new UserService();
        private User user;
        
        private string _Socname;
        /// <summary>
        /// 构造函数
        /// </summary>
        public TimelineViewModel(string name)
        {
            _Socname=name;
            Timeline = new ObservableCollection<Status>();
            if (name == "tencent")
            {
                tencentfactory = TencentSocFactory.Instance;
                _tencentAPIService = tencentfactory.GetAPIService();
                return;
            }
            if (name == "sina")
            {
                sinafactory = SinaSocFactory.Instance;
                _sinaAPIService = sinafactory.GetAPIService();
                return;
            }

            if (name == "renren")
            {
                renrenfactory = RenRenSocFactory.Instance;
                _renrenAPIService = renrenfactory.GetAPIService();
                return;
            }
        }

        /// <summary>
        /// 刷新Timeline View所需要的数据
        /// </summary>
        /// <param name="action">刷新完成后，回调到UI线程 /param>
        public void Refresh(Action action)
        {
            // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
                {
                    // 获得自己的详细资料.
                    if (_Socname == "tencent")
                    {
                        TencentRefresh(action);
                        return;
                    }
                    if (_Socname == "sina")
                    {
                        SinaRefresh(action);
                        return;
                    }

                    if (_Socname == "renren")
                    {
                        RenRenRefresh(action);
                        return;
                    }
                 
                }).Start();
        }

        
        
        /// <summary>
        /// 获取或设置当前登陆用户
        /// </summary>
        public User User
        {
            get { return user; }
            set
            {
                if (user != value)
                {
                    user = value;
                    NotifyPropertyChanged("User");
                }
            }
        }


        private void TencentRefresh(Action action)
        {
            _tencentAPIService.UserInfo((userCallback) =>
            {
                // Callback 结果是当前 AccessToken 的用户数据
                // 可以参考 UserInfo 的定义: UserInfo(Action<Callback<User>> callback)
                this.User = userCallback.Data;

                // 刷新Timeline, API的参数是通过ServiceArugment传递，只需要填入你所需要的参数
                // 同样，callback的数据类型为: StatusCollection, 参考该API的定义： 
                // HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback);
                _tencentAPIService.HomeTimeline(new TencentServiceArgument() { Reqnum = 30 }, (callback) =>
                {
                    // 因为回调回来之后会是后台线程，需要切换到UI线程去处理ObservableCollection
                    // 相关的操作
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // 清空Timeline，并把回调回来的数据添加到Timeline里去.
                        Timeline.Clear();
                        callback.Data.ForEach(a => Timeline.Add(a));

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action();
                            }
                        });
                    });
                });
            });
        }


        private void SinaRefresh(Action action)
        {
          

                // 刷新Timeline, API的参数是通过ServiceArugment传递，只需要填入你所需要的参数
                // 同样，callback的数据类型为: StatusCollection, 参考该API的定义： 
                // HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback);
                _sinaAPIService.HomeTimeline(new SinaServiceArgument(), (callback) =>
                {
                    // 因为回调回来之后会是后台线程，需要切换到UI线程去处理ObservableCollection
                    // 相关的操作
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // 清空Timeline，并把回调回来的数据添加到Timeline里去.
                        Timeline.Clear();
                        callback.Data.ForEach(a => Timeline.Add(a));

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action();
                            }
                        });
                    });
                });
         
        }


        private void RenRenRefresh(Action action)
        {


            // 刷新Timeline, API的参数是通过ServiceArugment传递，只需要填入你所需要的参数
            // 同样，callback的数据类型为: StatusCollection, 参考该API的定义： 
            // HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback);
            _renrenAPIService.HomeTimeline(new RenRenServiceArgument(), (callback) =>
            {
                // 因为回调回来之后会是后台线程，需要切换到UI线程去处理ObservableCollection
                // 相关的操作
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // 清空Timeline，并把回调回来的数据添加到Timeline里去.
                    Timeline.Clear();
                    callback.Data.ForEach(a => Timeline.Add(a));

                    // 切换并回调到UI线程.
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (null != action)
                        {
                            action();
                        }
                    });
                });
            });

        }

        /// <summary>
        /// 获取或设置首页时间线
        /// </summary>
        public ObservableCollection<Status> Timeline
        {
            get;
            set;
        }
    }
}
